let pageNum = 1;
let totalPage;
$(function () {
    $.ajax({
        url:"/inboundStatics/getAllRk",
        type: "POST",
        data:{},
        dataType:"json",
        success:function (r) {
            // console.log(r);
            if (r!=null){
                var t = "";
                var i = 1;
                for (let j = 0; j < r.data.length; j++) {
                    t += "<tr>\n" +
                        "          <td>"+(i++)+"</td>\n" +
                        "          <td class='warename' id='"+r.data[j].warehousename+"'>"+r.data[j].warehousename+"</td>\n" +
                        "          <td class='uname' id='"+r.data[j].uname+"'>"+r.data[j].uname+"</td>\n" +
                        "          <td>"+r.data[j].provincename+r.data[j].cityname+"</td>\n" +
                        "          <td>"+r.data[j].rknum+"</td>\n" +
                        "          <td>"+r.data[j].totalprice+"</td>\n" +
                        "          <td>\n" +
                        "          \t<a href=\"javascript:void(0)\" onclick='toStockView(this)' class=\"tablelink\">查看详情</a>\n" +
                        "          </td>\n" +
                        "        </tr>";
                }
                $(".tablelist tbody").html(t);
                $(".message>i").eq(0).html(r.totalRows);
                $(".message>i").eq(1).html(r.pageNum);
                $(".paginList").html("<li class=\"paginItem\"><a href=\"javascript:;\"><span class=\"pagepre\" onclick=\"prevPage()\"></span></a></li><li class=\"paginItem\"><a href=\"javascript:;\"><span class=\"pagenxt\" onclick=\"nextPage()\"></span></a></li>")
                totalPage = r.totalPage;
                nextPage();
                prevPage();
            }else {
                $(".tablelist tbody").html("");
            }
        }
    })

    $.ajax({
        url:"/inboundStatics/getProvince",
        type:"post",
        dataType:"json",
        success:function (r) {
            console.log(r)
            let str = "<option>请选择省份</option>";
            for (let i = 0; i < r.length; i++) {
                str += "<option id='"+r[i].pname+"'>"+r[i].pname+"</option>"
            }
            $(".tools>li>select").eq(0).html(str);
        }
    })
})

function toStockView(a) {
    var warehousename = $(a).parents().siblings(".warename").attr("id");
    var uname = $(a).parents().siblings(".uname").attr("id");

    $.ajax({
        url: "/inboundStatics/toStockView",
        type: "POST",
        data: {"warehousename":warehousename,"uname":uname},
        dataType: "json",
        success:function (r) {
                location.href = "stockView.html";
        }
    })
}

function queryWareHouseByCondition(a) {
    var pname = $(a).siblings(".address").children().eq(0).val();
    var city = $(a).siblings(".address").children().eq(1).val();
    var warehouname = $(a).siblings(".warehouse").children().val();
    var startTime = $(a).siblings(".time").children().eq(0).val();
    var endTime = $(a).siblings(".time").children().eq(1).val();
    // console.log(pname);
    // console.log(city);
    // console.log(warehouname);
    // console.log(startTime);
    // console.log(endTime);
    $.ajax({
        url:"/inboundStatics/queryWareHouseByCondition",
        type:"post",
        data:{"pname":pname,"city":city,"warehouname":warehouname,"startTime":startTime,"endTime":endTime},
        dataType:"json",
        success:function (r) {
            console.log(r)
            var t = "";
            var i = 1;
            if (r!=null) {
                for (let j = 0; j < r.data.length; j++) {
                    t += "<tr>\n" +
                        "          <td>" + (i++) + "</td>\n" +
                        "          <td class='warename' id='" + r.data[j].warehousename + "'>" + r.data[j].warehousename + "</td>\n" +
                        "          <td class='uname' id='" + r.data[j].uname + "'>" + r.data[j].uname + "</td>\n" +
                        "          <td>" + r.data[j].provincename + r.data[j].cityname + "</td>\n" +
                        "          <td>" + r.data[j].rknum + "</td>\n" +
                        "          <td>" + r.data[j].totalprice + "</td>\n" +
                        "          <td>\n" +
                        "          \t<a href=\"javascript:void(0)\" onclick='toStockView(this)' class=\"tablelink\">查看详情</a>\n" +
                        "          </td>\n" +
                        "        </tr>";
                }
                $(".tablelist tbody").html(t);
                $(".message>i").eq(0).html(r.totalRows);
                $(".message>i").eq(1).html(r.pageNum);
                totalPage = r.totalPage;
                $(".paginList").html("<li class=\"paginItem\"><a href=\"javascript:;\"><span class=\"pagepre\" onclick=\"prevPage1(this)\"></span></a></li><li class=\"paginItem\"><a href=\"javascript:;\"><span class=\"pagenxt\" onclick=\"nextPage1(this)\"></span></a></li>")
            }
            else {
                $(".tablelist tbody").html("空");
            }

        }

    })
}


function getCity(a) {
    var pname = $(a).val();
    $.ajax({
        url: "/inboundStatics/getCity",
        type: "post",
        data:{"pname":pname},
        dataType: "json",
        success: function (r) {
            console.log(r)
            let str = "<option>请选择城市</option>";
            for (let i = 0; i < r.length; i++) {
                str += "<option id='" + r[i].cityname + "'>" + r[i].cityname + "</option>"
            }
            $(".tools>li>select").eq(1).html(str);
        }
    })
}


function nextPage() {
    if (pageNum<totalPage){
        pageNum++;
    }
    $.ajax({
        url:"/inboundStatics/getAllRk",
        type: "POST",
        data:{"pageNum":pageNum},
        dataType:"json",
        success:function (r) {
            // console.log(r);
            var t = "";
            var i = 1;
            for (let j = 0; j < r.data.length; j++) {
                t += "<tr>\n" +
                    "          <td>"+(i++)+"</td>\n" +
                    "          <td class='warename' id='"+r.data[j].warehousename+"'>"+r.data[j].warehousename+"</td>\n" +
                    "          <td class='uname' id='"+r.data[j].uname+"'>"+r.data[j].uname+"</td>\n" +
                    "          <td>"+r.data[j].provincename+r.data[j].cityname+"</td>\n" +
                    "          <td>"+r.data[j].rknum+"</td>\n" +
                    "          <td>"+r.data[j].totalprice+"</td>\n" +
                    "          <td>\n" +
                    "          \t<a href=\"javascript:void(0)\" onclick='toStockView(this)' class=\"tablelink\">查看详情</a>\n" +
                    "          </td>\n" +
                    "        </tr>";
            }
            $(".tablelist tbody").html(t);
            $(".message>i").eq(0).html(r.totalRows);
            $(".message>i").eq(1).html(r.pageNum);
        }
    })
}

function prevPage() {
    if (pageNum>1){
        pageNum--;
    }
    $.ajax({
        url:"/inboundStatics/getAllRk",
        type: "POST",
        data:{"pageNum":pageNum},
        dataType:"json",
        success:function (r) {
            // console.log(r);
            var t = "";
            var i = 1;
            for (let j = 0; j < r.data.length; j++) {
                t += "<tr>\n" +
                    "          <td>"+(i++)+"</td>\n" +
                    "          <td class='warename' id='"+r.data[j].warehousename+"'>"+r.data[j].warehousename+"</td>\n" +
                    "          <td class='uname' id='"+r.data[j].uname+"'>"+r.data[j].uname+"</td>\n" +
                    "          <td>"+r.data[j].provincename+r.data[j].cityname+"</td>\n" +
                    "          <td>"+r.data[j].rknum+"</td>\n" +
                    "          <td>"+r.data[j].totalprice+"</td>\n" +
                    "          <td>\n" +
                    "          \t<a href=\"javascript:void(0)\" onclick='toStockView(this)' class=\"tablelink\">查看详情</a>\n" +
                    "          </td>\n" +
                    "        </tr>";
            }
            $(".tablelist tbody").html(t);
            $(".message>i").eq(0).html(r.totalRows);
            $(".message>i").eq(1).html(r.pageNum);
        }
    })
}

function nextPage1() {
    if (pageNum<totalPage){
        pageNum++;
    }
    var pname = $(".address").children().eq(0).val();
    var city = $(".address").children().eq(1).val();
    var warehouname = $(".warehouse").children().val();
    var startTime = $(".time").children().eq(0).val();
    var endTime = $(".time").children().eq(1).val();
    // console.log(pname);
    // console.log(city);
    // console.log(warehouname);
    // console.log(startTime);
    // console.log(endTime);
    $.ajax({
        url:"/inboundStatics/queryWareHouseByCondition",
        type:"post",
        data:{"pageNum":pageNum,"pname":pname,"city":city,"warehouname":warehouname,"startTime":startTime,"endTime":endTime},
        dataType:"json",
        success:function (r) {
            console.log(r)
            var t = "";
            var i = 1;
            if (r!=null) {
                for (let j = 0; j < r.data.length; j++) {
                    t += "<tr>\n" +
                        "          <td>" + (i++) + "</td>\n" +
                        "          <td class='warename' id='" + r.data[j].warehousename + "'>" + r.data[j].warehousename + "</td>\n" +
                        "          <td class='uname' id='" + r.data[j].uname + "'>" + r.data[j].uname + "</td>\n" +
                        "          <td>" + r.data[j].provincename + r.data[j].cityname + "</td>\n" +
                        "          <td>" + r.data[j].rknum + "</td>\n" +
                        "          <td>" + r.data[j].totalprice + "</td>\n" +
                        "          <td>\n" +
                        "          \t<a href=\"javascript:void(0)\" onclick='toStockView(this)' class=\"tablelink\">查看详情</a>\n" +
                        "          </td>\n" +
                        "        </tr>";
                }
                $(".tablelist tbody").html(t);
                $(".message>i").eq(0).html(r.totalRows);
                $(".message>i").eq(1).html(r.pageNum);
                $(".paginList").html("<li class=\"paginItem\"><a href=\"javascript:;\"><span class=\"pagepre\" onclick=\"prevPage1(this)\"></span></a></li><li class=\"paginItem\"><a href=\"javascript:;\"><span class=\"pagenxt\" onclick=\"nextPage1(this)\"></span></a></li>")
            }
            // else {
            //     $(".tablelist tbody").html("空");
            // }
        }

    })
}
function prevPage1() {
    if (pageNum>1){
        pageNum--;
    }
    var pname = $(".address").children().eq(0).val();
    var city = $(".address").children().eq(1).val();
    var warehouname = $(".warehouse").children().val();
    var startTime = $(".time").children().eq(0).val();
    var endTime = $(".time").children().eq(1).val();
    console.log(pname);
    console.log(city);
    console.log(warehouname);
    console.log(startTime);
    console.log(endTime);
    $.ajax({
        url:"/inboundStatics/queryWareHouseByCondition",
        type:"post",
        data:{"pageNum":pageNum,"pname":pname,"city":city,"warehouname":warehouname,"startTime":startTime,"endTime":endTime},
        dataType:"json",
        success:function (r) {
            console.log(r)
            var t = "";
            var i = 1;
            if (r!=null) {
                for (let j = 0; j < r.data.length; j++) {
                    t += "<tr>\n" +
                        "          <td>" + (i++) + "</td>\n" +
                        "          <td class='warename' id='" + r.data[j].warehousename + "'>" + r.data[j].warehousename + "</td>\n" +
                        "          <td class='uname' id='" + r.data[j].uname + "'>" + r.data[j].uname + "</td>\n" +
                        "          <td>" + r.data[j].provincename + r.data[j].cityname + "</td>\n" +
                        "          <td>" + r.data[j].rknum + "</td>\n" +
                        "          <td>" + r.data[j].totalprice + "</td>\n" +
                        "          <td>\n" +
                        "          \t<a href=\"javascript:void(0)\" onclick='toStockView(this)' class=\"tablelink\">查看详情</a>\n" +
                        "          </td>\n" +
                        "        </tr>";
                }
                $(".tablelist tbody").html(t);
                $(".message>i").eq(0).html(r.totalRows);
                $(".message>i").eq(1).html(r.pageNum);
                $(".paginList").html("<li class=\"paginItem\"><a href=\"javascript:;\"><span class=\"pagepre\" onclick=\"prevPage1(this)\"></span></a></li><li class=\"paginItem\"><a href=\"javascript:;\"><span class=\"pagenxt\" onclick=\"nextPage1(this)\"></span></a></li>")
            }
            // else {
            //     $(".tablelist tbody").html("空");
            // }
        }

    })
}
